package com.mdd.common.util.weifengqi;

import com.alibaba.fastjson.JSONObject;
import com.mdd.common.entity.yangyangLeadingEnterprise.dto.ReportDTO;
import com.mdd.common.util.HttpUtils;
import com.mdd.common.util.IntegerUtil;
import com.mdd.common.util.weifengqi.report.*;
import org.apache.commons.math3.util.Pair;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WFQUtil {
    //取报告---渠道编码号
    public static final String interfaceCode = "a6343b19103946a28401e3329b2f6899HA2TO";
    //取报告---加密串
    public static final String secretKey = "231ntTEDmFPlYKAJdteqK5We22jbd7TT2WESPFHPA/h1lQFp4mkBp6QZvik8D8N3";

    //贷前报告
    public static final String interfaceCode_dai = "ef911dbc71ee40c494f10b0a64d722a1ks438";
    public static final String secretKey_dai = "fjx30gaP2uKSy0k2AbLGqFV4zUUWQS0y3AlTfF/7oP2LYQiIrYT2JJD39cq4vL+Q";


    /**
     * 获取报告
     * @param reportType    报告类型
     * @param orderNo       订单号
     * @param taxNo         批次号
     * @param taxpayerId    公司税号
     *
     * @return
     */
    public static ReportDTO getReport(String reportType, String orderNo, String taxNo,String taxpayerId) {
        //入参
        Map<String, String> map = new HashMap<>();
        if("pre_loan_report".equals(reportType)){
            map.put("interfaceCode", interfaceCode_dai);
            map.put("secretKey", secretKey_dai);
            map.put("taxpayerId", taxpayerId);
            //单次
            if(orderNo!=null && !orderNo.isEmpty()){
                map.put("orderNo", orderNo);
            }
            //批次
            if(taxNo!=null && !taxNo.isEmpty()){
                map.put("taxNo", taxNo);
            }
        }else {
            map.put("interfaceCode", interfaceCode);
            map.put("secretKey", secretKey);
            map.put("taxpayer_id", taxpayerId);
            //单次
            if(orderNo!=null && !orderNo.isEmpty()){
                map.put("order_no", orderNo);
            }
            //批次
            if(taxNo!=null && !taxNo.isEmpty()){
                map.put("tax_no", taxNo);
            }
        }



        String urlParam="";
        //requestType   1：POST（raw-json）；2：GET(Params)；
        Integer requestType=0;
        switch (reportType){
            case "finance_service_analysis"://融资服务报告
                requestType=1;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/indexes/report_url";
                break;
            case "tax_risk_check_simple"://税务风险报告-简版
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/ai_tax/report/tax_risk_v2/simple/pdf";
                break;
            case "fiscal_subsidies_analysis"://财政补贴报告
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/fiscal_subsidies/report/fiscal_subsidies_analysis/document";
                break;
            case "jindiao_supplier_analysis"://供应商尽调报告
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/jindiao_supplier/report/jindiao_supplier_analysis/pdf";
                break;
            case "business_strategy_analysis"://经营参谋报告
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/business_strategy/report/business_strategy_analysis/pdf";
                break;
            case "jindiao_company_analysis"://企业尽调报告
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/jindiao/report/jindiao_company_analysis/pdf";
                break;
            case "tax_risk_check_professional"://税务风险报告-税优版
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/ai_tax/report/tax_risk_professional/pdf";
                break;
            case "tax_risk_check"://税务风险报告-通用版
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/ai_tax/report/tax_risk_v2/pdf";
                break;
            case "invest_jindiao_analysis"://投资尽调报告
                requestType=2;
                urlParam="https://api.enterpriseservice.cn/mini-program-index-api/invest_jindiao/report/invest_jindiao_analysis/document";
                break;
            case "pre_loan_report"://贷前报告
                requestType=2;
                urlParam="https://api.wfq2020.com/wuneng-web-api/v2/api/loan/before/report/v3/pdf";
                break;
//                Pair<Boolean, String> taxRiskReport = PreLoanReportUtil.getTaxRiskReport(orderNo, taxNo, taxpayerId);
        }
        ReportDTO dto = sendReport(requestType,reportType, urlParam, map);
        return dto;
    }

    /**
     * 发送请求
     * @param requestType
     * @param reportType
     * @param urlParam
     * @param map
     * @return
     */
    public static ReportDTO sendReport(Integer requestType,String reportType,String urlParam,  Map<String, String> map) {
        if(requestType==1){
            String result = HttpUtils.sendPostRawJson(urlParam, map);
            if(result!=null && !result.isEmpty()){
                JSONObject jsonObject = JSONObject.parseObject(result);
                Integer code = jsonObject.getInteger("code");
                if(code==200){
                    String report_url = jsonObject.getJSONObject("data").getString("report_url");
                    return ReportDTO.success(code,"success",result,report_url,null,reportType);
                }
                return ReportDTO.success(code,"fail",result,null,null,reportType);
            }
        }
        else if (requestType==2) {
//            String result = HttpUtils.sendGetParams(urlParam, map);
            String result = HttpUtils.sendGetParamsByUrl(urlParam, map);
            if(result!=null && !result.isEmpty()){
                JSONObject jsonObject = JSONObject.parseObject(result);
                Integer code = jsonObject.getInteger("code");
                if(code==200){
                    JSONObject data = jsonObject.getJSONObject("data");
                    //贷前报告
                    if(reportType.equals("pre_loan_report")){
                        String report_url = data.getString("reportPdfUrl");
                        return ReportDTO.success(code,"success",result,report_url,null,reportType);
                    }
                    //财政补贴报告    ||  税务风险报告-税优版  ||  投资尽调报告
                    if(reportType.equals("fiscal_subsidies_analysis") || reportType.equals("tax_risk_check_professional") || reportType.equals("invest_jindiao_analysis")){
                        String pdf = data.getString("pdf");
                        String doc = data.getString("doc");
                        return ReportDTO.success(code,"success",result,pdf,doc,reportType);
                    }
                    String report_url = data.getString("report_url");
                    return ReportDTO.success(code,"success",result,report_url,null,reportType);
                }
                return ReportDTO.success(code,"fail",result,null,null,reportType);
            }
        }
        return ReportDTO.success(500,"fail",null,null,null,reportType);
    }



    public static void main(String[] args) {
        String orderNo="JMlntDwhOyu4ihyhKgqkomfbV0WcqrqYyAM1";
        /**
         * "finance_service_analysis:融资服务报告"
         * "tax_risk_check:税务风险报告-通用版"
         * "tax_risk_check_simple:税务风险报告-简版"
         * "tax_risk_check_professional:税务风险报告-税优版"
         *  "jindiao_company_analysis:企业尽调报告"
         *  "fiscal_subsidies_analysis:财政补贴报告"
         *  "jindiao_supplier_analysis:供应商尽调报告"
         *  "business_strategy_analysis:经营参谋报告"
         *  "invest_jindiao_analysis:投资尽调报告"
         */
//        //融资服务报告
//        ReportDTO dto = getReport("finance_service_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //税务风险报告-通用版
//        ReportDTO dto = getReport("tax_risk_check", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //税务风险报告-简版
//        ReportDTO dto = getReport("tax_risk_check_simple", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //税务风险报告-税优版
//        ReportDTO dto = getReport("tax_risk_check_professional", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //企业尽调报告
//        ReportDTO dto = getReport("jindiao_company_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //财政补贴报告
//        ReportDTO dto = getReport("fiscal_subsidies_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //供应商尽调报告
//        ReportDTO dto = getReport("jindiao_supplier_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //经营参谋报告
//        ReportDTO dto = getReport("business_strategy_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
//        //投资尽调报告
//        ReportDTO dto = getReport("invest_jindiao_analysis", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
        //贷前报告
        ReportDTO dto = getReport("pre_loan_report", null, "TAX_NO_0120250603111935702","91110302MA017H9344");
        System.out.println(dto.toString());
    }

}
